package cn.edu.gzhmt.order.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.gzhmt.cart.domain.Cart;
import cn.edu.gzhmt.cart.domain.CartItem;
import cn.edu.gzhmt.order.domain.Order;
import cn.edu.gzhmt.order.domain.OrderItem;
import cn.edu.gzhmt.order.service.IOrderService;
import cn.edu.gzhmt.order.service.impl.OrderServiceImpl;
import cn.edu.gzhmt.user.domain.User;
import cn.edu.gzhmt.utils.commons.CommonUtils;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

/**
 * 订单模块 web层
 * @author azhuo
 *
 */
public class OrderServlet extends BaseServlet {

	private IOrderService orderService = new OrderServiceImpl();

	/**
	 * 生成订单
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String generateOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Cart cart = (Cart) request.getSession().getAttribute("session_cart");
		if (cart == null) {
			request.setAttribute("msg", "你是过来搞事情的吧？");
			return "f:/jsps/users/login.jsp";
		}

		String gids = request.getParameter("gids");
		if (gids == null) {
			return null;
		}
		String[] gidArr = gids.split(","); // 获取要进行下单的条目

		/* *****************************************************************
		 * 进行数据封装
		 */
		User user = (User) request.getSession().getAttribute("user");

		// 创建订单
		Order order = new Order();
		order.setOid(CommonUtils.uuid()); // 订单编号id
		order.setOstate("1"); // 订单状态：已经下单，未付款
		// order.setTotal(total); // 要进行计算订单中的总价
		order.setTime(new Date()); // 下单时间
		order.setUser(user); // 订单所属的用户
		order.setOaddress(user.getAddress()); // 订单收货地址

		List<OrderItem> orderItemList = new ArrayList<OrderItem>();
		// 根据购物车中商品生成订单
		for (String gid : gidArr) {

			CartItem cartItem = cart.getCartItem(gid);
			if (cartItem != null) {

				// 根据购物车中的条目 设置 订单中的条目
				OrderItem orderItem = new OrderItem();
				orderItem.setIid(CommonUtils.uuid());
				orderItem.setCount(cartItem.getCount());
				orderItem.setGoods(cartItem.getGoods());
				orderItem.setSubtotal(cartItem.getSubTotal());
				orderItem.setOrder(order);
				
				orderItemList.add(orderItem);

				// 商品添加到订单后，在购物车中删除条目
				cart.remove(gid);
			}
		}
		order.setOrderItemList(orderItemList);
		
		/* *****************************************************************
		 * 生成订单  
		 */
		orderService.generateOrder(order);

		// 在Session中更新购物车
		request.getSession().setAttribute("session_cart", cart);

		List<Order> orderList = new ArrayList<Order>();
		orderList.add(order);
		request.setAttribute("orderList", orderList);

		return "f:/jsps/order/orders.jsp";
	}

	/**
	 * 加载订单：根据订单id进行查询订单详情
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String oid = request.getParameter("oid");
		if (oid == null) {
			request.setAttribute("msg", "你是过来搞事情的吧？");
			return "f:/jsps/users/login.jsp";
		}

		// 根据订单id 加载订单
		Order order = orderService.loadOrder(oid);
		request.setAttribute("order", order);

		return "f:/jsps/order/ordersDetails.jsp";
	}

	/**
	 * 查看用户的所有订单
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			request.setAttribute("msg", "你是过来搞事情的吧？");
			return "f:/jsps/users/login.jsp";
		}

		String uid = user.getUid();

		// 查询用户的所有订单
		List<Order> orderList = orderService.findAllOrder(uid);
		request.setAttribute("orderList", orderList);

		return "f:/jsps/order/orders.jsp";
	}

	/**
	 * 订单付款
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String zhifu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oid = request.getParameter("oid");

		// 进行付款操作
		orderService.zhifu(oid);

		return this.findAllOrder(request, response);
	}

	/**
	 * 确认收货
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String confirm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String oid = request.getParameter("oid");

		// 进行确认收货操作
		orderService.confirm(oid);

		return this.findAllOrder(request, response);
	}

	/**
	 * 删除订单
	 * 为进行订单状态判断：所有状态下的订单都可以删除
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeOrder(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String oid = request.getParameter("oid");

		// 进行删除订单操作
		orderService.removeOrder(oid);

		return this.findAllOrder(request, response);
	}
}
